package manager.cms.service.impl;
import common.PageView;
import manager.cms.dao.CmsContentCatDao;
import manager.cms.dao.CmsContentDao;
import manager.cms.service.CmsContentService;
import manager.cms.service.JdExecutor;
import manager.cms.service.exec.ItemListExecImpl;
import manager.cms.service.exec.SearchGoodsExecImpl;
import manager.cms.vo.CmsContentCatVO;
import manager.cms.vo.CmsContentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "cmsContentService")
public class CmsContentServiceImpl implements CmsContentService {
    /**
     * cms分类信息
     */
    @Autowired
    private CmsContentCatDao cmsContentCatDao;
    /**
     * Cms内容信息
     */
    @Autowired
    private CmsContentDao cmsContentDao;
    @Autowired
    private JdExecutor focusMonetizeExec;
    @Autowired
    private JdExecutor topMonetizeExec;
    @Autowired
    private JdExecutor hotwordsExec;
    @Autowired
    private  JdExecutor todayMonetizeExec;
    @Autowired
    private JdExecutor rankCategoryExec;
    @Autowired
    private JdExecutor miaoShaGoodsExec;
    @Autowired
    private JdExecutor marketBillExec;
    @Autowired
    private JdExecutor lightningBuyExec;
    @Autowired
    private JdExecutor haoDianExec;
    @Autowired
    private JdExecutor discoverGoodsExec;
    @Autowired
    private JdExecutor dailySpeciTabExec;
    @Autowired
    private JdExecutor couponExec;
    @Autowired
    private JdExecutor newArrivalExec;
    @Autowired
    private JdExecutor activityExec;
    @Autowired
    private JdExecutor skuAsyncExec;
    @Autowired
    private ItemListExecImpl itemListExec;
    @Autowired
    private JdExecutor spuParamsTempleteExec;
    @Autowired
    private JdExecutor brandExec;
    @Autowired
    private SearchGoodsExecImpl searchGoodsExec;
    /**
     * 分页获取分类信息
     *
     * @param cmsContentCatVO
     * @return
     */
    @Override
    public PageView<List<CmsContentCatVO>> getCmsContentCatByPage(CmsContentCatVO cmsContentCatVO) {
        PageView<List<CmsContentCatVO>> pageView = new PageView<List<CmsContentCatVO>>();
        pageView.setRows(cmsContentCatDao.selectByConditionPage(cmsContentCatVO));
        pageView.setTotal(cmsContentCatDao.selectByConditionPageCount(cmsContentCatVO));
        return pageView;
    }

    /**
     * 不分页获取分类信息
     *
     * @param cmsContentCatVO
     * @return
     */
    @Override
    public List<CmsContentCatVO> getCmsContentCat(CmsContentCatVO cmsContentCatVO) {
        return cmsContentCatDao.selectByCondition(cmsContentCatVO);
    }
    @Override
    public PageView<List<CmsContentVO>> getCmsContentByPage(CmsContentVO cmsContentVO) {
        PageView<List<CmsContentVO>> pageView = new PageView<List<CmsContentVO>>();
        pageView.setRows(cmsContentDao.selectByConditionPage(cmsContentVO));
        pageView.setTotal(cmsContentDao.selectByConditionPageCount(cmsContentVO));
        return pageView;
    }
    @Override
    public List<CmsContentVO> getCmsContent(CmsContentVO cmsContentVO) {
        return cmsContentDao.selectByCondition(cmsContentVO);
    }

    @Override
    public void addCmsContentCan(CmsContentCatVO cmsContentCatVO) {
        cmsContentCatDao.addCmsContentCat(cmsContentCatVO);
    }

    @Override
    public void addCmsContentCatBatch(List<CmsContentCatVO> cmsContentCatList) {
        cmsContentCatDao.addCmsContentCatBatch(cmsContentCatList);
    }

    @Override
    public void addCmsContent(CmsContentVO cmsContentVO) {
        cmsContentDao.addCmsContent(cmsContentVO);
    }

    @Override
    public void addCmsContentBatch(List<CmsContentVO> cmsContentList) {
        cmsContentDao.addCmsContentBatch(cmsContentList);
    }

    @Override
    public void updateCmsContentCan(CmsContentCatVO cmsContentCatVO) {
        cmsContentCatDao.updateCmsContentCat(cmsContentCatVO);
    }

    @Override
    public void updateCmsContent(CmsContentVO cmsContentVO) {
        cmsContentDao.updateCmsContent(cmsContentVO);
    }

    @Override
    public void deleteCmsContentCanList(String[] ids) {
        cmsContentCatDao.deleteCmsContentCat(ids);
    }

    @Override
    public void deleteCmsContentList(String[] ids) {
        cmsContentDao.deleteCmsContent(ids);
    }

    /**
     * 同步京东商城首页信息
     */
    @Override
    public void asyncJdCmsData() {
        try {
            Map<String,Object> mapParams=new HashMap<String,Object>();
            //首先删除原来的数据
            cmsContentCatDao.deleteAllCmsContentCat();
            /**
             * 删除原来的数据
             */
            cmsContentDao.deleteAllCmsContent();
            /**
             * 获取活动数据
             */
            activityExec.execute(mapParams);
            //获取首页轮banner条
            topMonetizeExec.execute(mapParams);
            //获取首页轮播图
            focusMonetizeExec.execute(mapParams);
            //获取关键字信息
            hotwordsExec.execute(mapParams);
            /**
             * 获取右侧轮播图
             */
            todayMonetizeExec.execute(mapParams);
            /**
             * 获取排名商品
             */
            rankCategoryExec.execute(mapParams);
            /**
             * 频道
             */
            marketBillExec.execute(mapParams);
            /**
             * 品牌闪购
             */
            lightningBuyExec.execute(mapParams);
            /**
             * 好店
             */
             haoDianExec.execute(mapParams);
             //发现好货
             discoverGoodsExec.execute(mapParams);
            /**
             *获取特价商品
             */
            dailySpeciTabExec.execute(mapParams);
            /**
             * couponExec
             */
            couponExec.execute(mapParams);
            /**
             * 秒杀
             */
            miaoShaGoodsExec.execute(mapParams);
            /**
             * 新品
             */
            newArrivalExec.execute(mapParams);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    /**
     * 同步分类商品信息数据
     */
    @Override
    public void asyncJdCategoryGoods(Map<String,Object> categoryParams) {
        itemListExec.execute(categoryParams);
        List<String> skuIds=itemListExec.getSkuIds();
        Map<String,Object> mapParams=new HashMap<String,Object>();
        List<Map<String,String>> skuList=new ArrayList<Map<String,String>>();
        skuIds.forEach(skuId->{
            Map<String,String> map=new HashMap<String,String>();
            map.put("skuId",skuId);
            map.put("categoryId",(String)categoryParams.get("categoryId"));
            skuList.add(map);
        });
        mapParams=new HashMap<String,Object>();
        mapParams.put("skuList",skuList);
        skuAsyncExec.execute(mapParams);
    }

    @Override
    public void asyncJdCategoryTemplete(Map<String, Object> categoryInfo) {
        itemListExec.execute(categoryInfo);
        List<String> skuIds=itemListExec.getSkuIds();
        //获取搜索页面的搜索条件
        searchGoodsExec.execute(categoryInfo);
        Map<String,Object> resultMap=searchGoodsExec.getExecResult();
        //根据第一个skuId获取和设置通用规格参数和特殊规格参数模板信息
        Map<String,Object> mapParams=new HashMap<String,Object>();
        if(null!=skuIds&&skuIds.size()>0){
            mapParams.put("skuId",skuIds.get(0));
            mapParams.put("categoryId",(String)categoryInfo.get("categoryId"));
            mapParams.put("searchConditionMap",resultMap);
            spuParamsTempleteExec.execute(mapParams);
        }
    }

    @Override
    public void asyncJdBrand(Map<String, Object> categoryInfo) {
         brandExec.execute(categoryInfo);
    }
}



